-- Tags: no-parallel-replicas
{% for join_algorithm in ['hash', 'parallel_hash', 'full_sorting_merge', 'grace_hash'] -%}

SET max_rows_in_join = '{% if join_algorithm == 'grace_hash' %}10K{% else %}0{% endif %}';
SET grace_hash_join_initial_buckets = 4;

-- Test is slow with external sort / group by
SET max_bytes_before_external_sort = 0, max_bytes_before_external_group_by = 0;

SELECT '--- {{ join_algorithm }} ---';

SET join_algorithm = '{{ join_algorithm }}';

SELECT
    EventDate,
    hits,
    visits
FROM
(
    SELECT
        EventDate,
        count() AS hits
    FROM test.hits
    GROUP BY EventDate
) ANY LEFT JOIN
(
    SELECT
        StartDate AS EventDate,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY EventDate
) USING EventDate
ORDER BY hits DESC
LIMIT 10
SETTINGS joined_subquery_requires_alias = 0;


SELECT
    EventDate,
    count() AS hits,
    any(visits)
FROM test.hits ANY LEFT JOIN
(
    SELECT
        StartDate AS EventDate,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY EventDate
) USING EventDate
GROUP BY EventDate
ORDER BY hits DESC
LIMIT 10
SETTINGS joined_subquery_requires_alias = 0;


SELECT
    domain,
    hits,
    visits
FROM
(
    SELECT
        domain(URL) AS domain,
        count() AS hits
    FROM test.hits
    GROUP BY domain
) ANY LEFT JOIN
(
    SELECT
        domain(StartURL) AS domain,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY domain
) USING domain
ORDER BY hits DESC
LIMIT 10
SETTINGS joined_subquery_requires_alias = 0;

{% if join_algorithm not in ['full_sorting_merge', 'grace_hash'] -%}

SELECT CounterID FROM test.visits ARRAY JOIN Goals.ID WHERE CounterID = 942285 ORDER BY CounterID;

SELECT
    CounterID,
    hits,
    visits
FROM
(
    SELECT
        (CounterID % 100000) AS CounterID,
        count() AS hits
    FROM test.hits
    GROUP BY CounterID
) ANY FULL OUTER JOIN
(
    SELECT
        (CounterID % 100000) AS CounterID,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY CounterID
    HAVING visits > 0
) USING CounterID
WHERE hits = 0 OR visits = 0
ORDER BY
    hits + visits * 10 DESC,
    CounterID ASC
LIMIT 20
SETTINGS any_join_distinct_right_table_keys = 1, joined_subquery_requires_alias = 0;


SELECT
    CounterID,
    hits,
    visits
FROM
(
    SELECT
        (CounterID % 100000) AS CounterID,
        count() AS hits
    FROM test.hits
    GROUP BY CounterID
) ANY LEFT JOIN
(
    SELECT
        (CounterID % 100000) AS CounterID,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY CounterID
    HAVING visits > 0
) USING CounterID
WHERE hits = 0 OR visits = 0
ORDER BY
    hits + visits * 10 DESC,
    CounterID ASC
LIMIT 20
SETTINGS any_join_distinct_right_table_keys = 1, joined_subquery_requires_alias = 0;


SELECT
    CounterID,
    hits,
    visits
FROM
(
    SELECT
        (CounterID % 100000) AS CounterID,
        count() AS hits
    FROM test.hits
    GROUP BY CounterID
) ANY RIGHT JOIN
(
    SELECT
        (CounterID % 100000) AS CounterID,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY CounterID
    HAVING visits > 0
) USING CounterID
WHERE hits = 0 OR visits = 0
ORDER BY
    hits + visits * 10 DESC,
    CounterID ASC
LIMIT 20
SETTINGS any_join_distinct_right_table_keys = 1, joined_subquery_requires_alias = 0;


SELECT
    CounterID,
    hits,
    visits
FROM
(
    SELECT
        (CounterID % 100000) AS CounterID,
        count() AS hits
    FROM test.hits
    GROUP BY CounterID
) ANY INNER JOIN
(
    SELECT
        (CounterID % 100000) AS CounterID,
        sum(Sign) AS visits
    FROM test.visits
    GROUP BY CounterID
    HAVING visits > 0
) USING CounterID
WHERE hits = 0 OR visits = 0
ORDER BY
    hits + visits * 10 DESC,
    CounterID ASC
LIMIT 20
SETTINGS any_join_distinct_right_table_keys = 1, joined_subquery_requires_alias = 0;

SELECT UserID, pp.Key1, pp.Key2, ParsedParams.Key1 FROM test.hits ARRAY JOIN ParsedParams AS pp WHERE CounterID = 1704509 ORDER BY UserID, EventTime, pp.Key1, pp.Key2 LIMIT 100;

SELECT UserID, pp.Key1, pp.Key2, ParsedParams.Key1 FROM test.hits LEFT ARRAY JOIN ParsedParams AS pp WHERE CounterID = 1704509 ORDER BY UserID, EventTime, pp.Key1, pp.Key2 LIMIT 100;

SELECT a.*, b.* FROM
(
    SELECT number AS k FROM system.numbers LIMIT 10
) AS a
ANY INNER JOIN
(
    SELECT number * 2 AS k, number AS joined FROM system.numbers LIMIT 10
) AS b
USING k ORDER BY joined
SETTINGS any_join_distinct_right_table_keys = 1;

{% endif -%}

SELECT a.*, b.* FROM
(
    SELECT number AS k FROM system.numbers LIMIT 10
) AS a
ALL INNER JOIN
(
    SELECT intDiv(number, 2) AS k, number AS joined FROM system.numbers LIMIT 10
) AS b
USING k ORDER BY joined;

SET max_bytes_in_join = 0;

{% endfor -%}
